<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package  	PyroCMS
 * @subpackage  Categories
 * @category  	Module
 * @author  	PyroCMS Dev Team
 */
class Admin_Setting extends Admin_Controller {

	/**
	 * The current active section
	 * @access protected
	 * @var int
	 */
	protected $section = 'setting';
	/**
	 * Array that contains the validation rules
	 * @access protected
	 * @var array
	 */
	protected $validation_rules = array(
		array(
			'field' => 'number_of_winner',
			'label' => 'lang:stoepje_voucher_number_of_winner',
			'rules' => 'trim|required|integer'
		)
	);
	
	/**
	 * The constructor
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('stoepje_setting_m');
		$this->lang->load('voucher');
		$this->config->load('voucher');
		
		date_default_timezone_set('Europe/Amsterdam');
		//date_default_timezone_set('Asia/Ho_Chi_Minh');
		
		
		// Load the validation library along with the rules
		$this->load->library('form_validation');
		$this->form_validation->set_rules($this->validation_rules);
		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
						->append_metadata(js('stoepje.voucher.js', 'voucher'))
						->append_metadata(css('stoepje.voucher.css', 'voucher'));
	}
	
	/**
	 * Index method, lists all categories
	 * @access public
	 * @return void
	 */
	public function index()
	{
		$this->pyrocache->delete_all('modules_m');

		$data = array();
		$data['hourOptions'] = array();
		for($i = 0; $i<12; $i++){
			$data['hourOptions'][$i] = $i;
		}
		$data['minuteOptions'] = array();
		for($i = 0; $i<60; $i++){
			$data['minuteOptions'][$i] = $i;
		}
		$data['periodOptions'] = array(
			'AM' => 'AM',
			'PM' => 'PM'
		);

		if ($this->form_validation->run()){
			$activeSettingValue = $this->input->post('active_setting');
			$activeSetting = $this->stoepje_setting_m->getByKey('active_setting');
			if($activeSetting){
				$this->stoepje_setting_m->update($activeSetting->id, array(
					'value' => $activeSettingValue . ''
				));
			}else{
				$this->stoepje_setting_m->insert(array(
					'key' => 'active_setting',
					'value' => $activeSettingValue . ''
				));
			}
			
			
			$fromHour = $this->input->post('from_hour');
			$fromMinute = $this->input->post('from_minute');
			$fromPeriod = $this->input->post('from_period');
			if($fromPeriod == 'PM'){
				$fromHour = $fromHour + 12;
			}
			$fromTimeValue = strtotime(date('j') . '-' . date('n') . '-'. date('Y') . ' ' . $fromHour . ':' . $fromMinute . ':0');
			
			
			$fromTime = $this->stoepje_setting_m->getByKey('from_time');
			if($fromTime){
				$this->stoepje_setting_m->update($fromTime->id, array(
					'value' => $fromTimeValue . ''
				));
			}else{
				$this->stoepje_setting_m->insert(array(
					'key' => 'from_time',
					'value' => $fromTimeValue . ''
				));
			}
			
			$toHour = $this->input->post('to_hour');
			$toMinute = $this->input->post('to_minute');
			$toPeriod = $this->input->post('to_period');
			if($toPeriod == 'PM'){
				$toHour = $toHour + 12;
			}
			
			$toTimeValue = strtotime(date('j') . '-' . date('n') . '-'. date('Y') . ' ' . $toHour . ':' . $toMinute . ':0');
			
			
			$toTime = $this->stoepje_setting_m->getByKey('to_time');
			if($toTime){
				$this->stoepje_setting_m->update($toTime->id, array(
					'value' => $toTimeValue . ''
				));
			}else{
				$this->stoepje_setting_m->insert(array(
					'key' => 'to_time',
					'value' => $toTimeValue . ''
				));
			}
			
			
			$numberOfWinner = $this->stoepje_setting_m->getByKey('number_of_winner');
			if($numberOfWinner){
				$this->stoepje_setting_m->update($numberOfWinner->id, array('value' => $this->input->post('number_of_winner')));
			}else{
				$this->stoepje_setting_m->insert(array(
					'key' => 'number_of_winner',
					'value' => $this->input->post('number_of_winner')
				));
			}
		}
		
		$data['activeSetting'] = $this->stoepje_setting_m->getByKey('active_setting');
		
		$fromTime = $this->stoepje_setting_m->getByKey('from_time');
		if($fromTime){
			$data['fromHour'] = date('g', (int)$fromTime->value);
			$data['fromMinute'] = (int)date('i', (int)$fromTime->value);
			$data['fromPeriod'] = date('A', (int)$fromTime->value);
		}else{
			$data['fromHour'] = 0;
			$data['fromMinute'] = 0;
			$data['fromPeriod'] = 'AM';
		}
		
		$toTime = $this->stoepje_setting_m->getByKey('to_time');
		if($toTime){
			$data['toHour'] = date('g', (int)$toTime->value);
			$data['toMinute'] = (int)date('i', (int)$toTime->value);
			$data['toPeriod'] = date('A', (int)$toTime->value);
		}else{
			$data['toHour'] = 0;
			$data['toMinute'] = 0;
			$data['toPeriod'] = 'AM';
		}
		
		$data['numberOfWinner'] = $this->stoepje_setting_m->getByKey('number_of_winner');
		
		$this->template
			->title($this->module_details['name'], lang('stoepje_voucher_setting_title'))
			->build('admin/setting/index', $data);
	}
}